<?php
namespace app\kaoping\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
use app\common\model\Smscode;
class Index extends First{
	protected $middleware = ['app\kaoping\middleware\CheckLogin::class'];
	public function index(){
		return view();
	}
	public function welcome(){
		//登录次数
		$list=Db::table('login_log')->where('aid',session('aid'))->order('time desc')->paginate(10);
		return view('',['list'=>$list]);
	}
	//管理员列表
	public function admin_list(){
		$where='';
		if (input('stime')!='') {
			$where.='a.time>='.strtotime(input('stime'));
		}
		if (input('etime')!='') {
			if ($where!='') {
				$where.=' and a.time<='.strtotime(input('etime'));
			}else{
				$where.='a.time<='.strtotime(input('etime'));
			}
		}
		if (input('type')!==null && input('type')!='') {
			if ($where!='') {
				$where.=' and a.type='.input('type');
			}else{
				$where.='a.type='.input('type');
			}
		}
		if (input('keys')!='') {
			if ($where!='') {
				$where.=' and s.name like "%'.input('keys').'%"';
			}else{
				$where.='s.name like "%'.input('keys').'%"';
			}
		}
		$query['stime']=input('stime');$query['etime']=input('etime');$query['type']=input('type');
		$list=Db::table('admin')
			->alias('a')
			->join('school s','a.id=s.aid','left')
			->join('admin kg','s.kg_id=kg.id','left')
			->where($where)
			->order('a.id asc')
			->field('a.*,s.name as sname,s.id as sid,s.files,s.address,kg.real_name as kg_name,s.kg_id')
			//->fetchsql()
			->paginate(['list_rows'=>10,'query'=>$query]);
		return view('',['stime'=>input('stime'),'etime'=>input('etime'),'type'=>input('type'),'keys'=>input('keys'),'list'=>$list]);
	}
	//获取考官列表
	public function get_kg(){
		if (request()->isPost()) {
			$kg=Db::table('admin')->where('type',2)->select();
			if ($kg) {
				$mes['code']=200;
				$mes['info']=$kg;
			}else{
				$mes['code']=400;
				$mes['info']=array();
			}
			return json($mes);
		}
	}
	//给机构设置考官
	public function school_addAid(){
		if (request()->isPost()) {
			//return json(input());
			if(input('sid')!='' && input('aid')!=''){
				$res=Db::table('school')->where('id',input('sid'))->update(['kg_id'=>input('aid')]);
				if ($res!==false) {
					$mes['code']=200;
					$mes['msg']='考官添加成功';
				}else{
					$mes['code']=400;
					$mes['msg']='考官添加失败';
				}
			}else{
				$mes['code']=400;
				$mes['msg']='请选择考官';
			}
			return json($mes);
		}
	}
	//管理员添加
	public function admin_add(){
		$id=input('id');
		$info=Db::table('admin')->alias('a')->leftjoin('school s','a.id=s.aid')->where('a.id',$id)->field('a.id,a.name,a.real_name,a.tel,a.hidden,a.type,s.id as sid,s.address')->find();//dump($info);die;
		if (request()->isPost()) {
			if(input('name') && input('password') && input('password2')){
				$data['name']=input('name');
				$data['password']=md5(md5(input('password')));
				if (input('password')!=input('password2')) {
					$this->error('两次输入密码不一致','','',1);
				}
				$data['paw']=input('password');
				input('hidden')!==''?$data['hidden']=input('hidden'):'';
				input('type')!=''?$data['type']=input('type'):'';
				input('real_name')!=''?$data['real_name']=input('real_name'):'';
				if(input('tel')!=''){
					if(is_mobile(input('tel'))){
						$data['tel']=input('tel');
					}else{
						die('手机号格式错误');
					}
				}
				if ($id!='') {
					$data['id']=$id;
					$res=Db::table('admin')->update($data);
				}else{
					$data['time']=time();
					$res=Db::table('admin')->insertgetid($data);
					if ($res and input('type')==1) {
						if (input('addType')==1) {
							$arr['aid']=$res;
							@Db::table('school')->insert($arr);
							//$sms=model('Smscode');
							$templateid = "385168";    //可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
							$param = input('name').','.input('password'); //多个参数使用英文逗号隔开（如：param=“a,b,c”），如为参数则留空
							//70字内（含70字）计一条，超过70字，按67字/条计费，超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
							@$s=Smscode::code($templateid,$param,$mobile=input('tel'));//dump($s);die;
						}else{
							$arr['id']=input('sname');
							$arr['aid']=$res;
							@Db::table('school')->update($arr);
						}
					}
				}
				if ($res!==false) {
					return redirect('admin_list');
				}else{
					return error('操作失败');
				}
			}
		}else{
			$school=Db::table('school')->where('aid',null)->field('id,name,tel')->select();
			return view('',['school'=>$school,'info'=>$info]);
		}
	}
	function sms_test(){
		$sms=model('Smscode');
		$templateid = "385168";    //可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
		$param = "name,password"; //多个参数使用英文逗号隔开（如：param=“a,b,c”），如为参数则留空
		//70字内（含70字）计一条，超过70字，按67字/条计费，超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
		$res=$sms->code($templateid,$param,$mobile=15838501447);
		dump($res);
	}
	//删除账号
	function admin_del(){
		if (request()->isPost()) {
			if(session('atype')!==0){
				return json(['code'=>400,'msg'=>'没有权限进行此操作']);
			}
			$id=input('id');
			if(is_array($id)){
				$where = 'id in('.implode(',',$id).')';
			}else{
				$where = 'id in('.$id.')';
			}
			$list=Db::table('admin')->where($where)->select();
			if ($list) {
				foreach ($list as $k => $v) {
					if ($v['type']==0) {
						$mes['state']=400;
						$mes['msg']='超级管理员不能删除';
						return $mes;
					}
					if ($v['type']==1) {
						@Db::table('school')->where('aid',$v['id'])->delete();
					}
					if($v['type']==2){
						@Db::table('school')->where('kg_id',$v['id'])->setField('kg_id','');
					}
				}
				$res=Db::table('admin')->where($where)->delete();
				if ($res) {
					$mes['state']=200;
					$mes['msg']='成功删除'.$res.'条数据';
				}else{
					$mes['state']=400;
					$mes['msg']='删除失败';
				}
			}else{
				$mes['state']=400;
				$mes['msg']='未找到相关数据';
			}
			return json($mes);
		}
	}
	//启用&&禁用账号
	public function admin_state(){
		if (request()->isPost()) {
			$id=input('id');
			if ($id!='') {
				$info=Db::table('admin')->where('id',$id)->find();
				if ($info) {
					if ($info['hidden']==0) {
						$res=Db::table('admin')->where('id',$id)->update(['hidden'=>1]);
						$mes['info']=1;
						$mes['msg']='账号已停用';
					}else{
						$res=Db::table('admin')->where('id',$id)->update(['hidden'=>0]);
						$mes['info']=0;
						$mes['msg']='账号已启用';
					}
					if ($res) {
						$mes['state']=200;
					}else{
						$mes['state']=400;
						$mes['msg']='操作失败';
					}
				}else{
					$mes['state']=400;
					$mes['msg']='未找到用户信息';
				}
			}else{
				$mes['state']=400;
				$mes['msg']='未找到用户信息';
			}
			return json($mes);
		}
	}
	//机构登录个人中心
	public function ainfo(){
		$info=Db::table('school')->where('aid',session('aid'))->find();
		if (request()->isPost()) {
			$data['name']=input('name');
			$data['contact']=input('contact');
			$data['tel']=input('tel');
			$data['email']=input('email');
			$data['address']=input('address');
			$data['id']=input('id');
			if (request()->file('files')) {
				$img=upload(request()->file('files'),'school');
				if ($img['status']==1) {
					if ($info['files']!='' && file_exists('.'.$info['files'])) @unlink('.'.$info['files']);
					$data['files']=$img['info'];
				}
			}
			$res=Db::table('school')->update($data);
			if ($res!==false) {
				return redirect('ainfo');
			}else{
				error('资料修改失败');
			}
		}else{
			return view('',['info'=>$info]);
		}
	}
	//批次管理
	public function pici(){
		if(request()->isPost()){
			if (input('post.name')!='') {
				$data=input('post.');
				if (input('id')!='') {
					$res=Db::table('pici')->update($data);
				}else{
					$data['aid']=session('aid');
					$data['time']=time();
					unset($data['id']);
					$res=Db::table('pici')->insert($data);
				}
				if ($res!==false) {
					return json(['code'=>200,'msg'=>'操作成功']);
				}else{
					return json(['code'=>400,'msg'=>'操作失败']);
				}
			}else{
				return json(['code'=>400,'msg'=>'批次名称不能为空！']);
			}
		}else{
			$list=Db::table('pici')->where('aid='.session('aid'))->order('id desc')->paginate(15);
			return view('',['list'=>$list]);
		}
	}
	//订单管理
	public function order(){
		if(session('atype')!=1){
			if(request()->isPost()){
				$id=input('id');//return $id;
				if($id!=''){
					$info=Db::table('pici')->where('id',$id)->find();
					if($info){
						if($info['real_money']>50000){
							$res=Db::table('pici')->where('id',$id)->update(['pay_state'=>1,'pay_type'=>1]);
							if($res){
								return json(['code'=>200,'msg'=>'操作成功']);
							}else{
								return json(['code'=>400,'msg'=>'操作失败']);
							}
						}else{
							return json(['code'=>400,'msg'=>'订单金额未满5万元不能进行此操作']);
						}
					}else{
						return json(['code'=>400,'msg'=>'未找到相关数据信息']);
					}
				}
			}else{
				$list=Db::table('pici')
					->alias('p')
					->join('school s','p.aid=s.aid','inner')
					->field('p.id,s.name as sname,p.name as pname,p.people_num,p.all_money,p.real_money,p.ordernum,p.pay_state,p.pay_type,p.time')
					->order('p.pay_state desc,p.time desc')
					->paginate(15);
				return view('',['list'=>$list]);
			}
		}else{
			die('页面不存在');
		}
	}
	//考官
	public function kaoguan(){
		if (request()->isPost()) {
			$data['aid']=session('aid');
			$data['name']=input('name');
			if (input('id')!='') {
				$data['id']=input('id');
				$res=Db::table('kaoguan')->update($data);
			}else{
				$res=Db::table('kaoguan')->insert($data);
			}
			if ($res) {
				$mes['code']=200;
				$mes['msg']='考官信息设置成功';
			}else{
				$mes['code']=400;
				$mes['msg']='错误';
			}
			return json($mes);
		}else{
			$list=Db::table('kaoguan')->select();
			return view('',['list'=>$list]);
		}
	}
	//考级考牌信息设置
	public function card(){
		if (request()->isPost()) {
			$data['type']=input('type');
			$data['name']=input('name');
			$data['money']=input('money');
			if (input('id')!='') {
				$data['id']=input('id');
				$res=Db::table('card')->update($data);
			}else{
				$res=Db::table('card')->insert($data);
			}
			if ($res) {
				$mes['code']=200;
				$mes['msg']='考级考牌信息设置成功';
			}else{
				$mes['code']=400;
				$mes['msg']='考级考牌信息设置出错';
			}
			return json($mes);
		}else{
			$where=array();
			if(input('type')!=''){
				$where['type']=array('eq',input('type'));
			}
			$list=Db::table('card')->where($where)->select();
			return view('',['type'=>input('type'),'list'=>$list]);
		}
	}
	//培训机构审核列表
	public function apply(){
		if(session('atype') === 0){
			$list=Db::table('apply')->order('state asc,time desc')->paginate(10);
			return view('',['list'=>$list]);
		}else{
			die('没有权限访问此栏目');
		}
	}
	//通过&&拒绝审核
	public function apply_state(){
		if (request()->isPost()) {
			if (input('id')!='' && input('state')!='') {
				if (input('state')==1) {
					$info=Db::table('admin')->where('name',input('name'))->find();
					if ($info) {
						$mes['code']=400;
						$mes['msg']='用户名已存在，请重新填写';
						return json($mes);
					}
				}
				$res=Db::table('apply')->where('id',input('id'))->update(['state'=>input('state')]);
				if ($res) {
					$mes['code']=200;
					$mes['msg']='审核成功';
					if(input('state')==1){
						$info=Db::table('apply')->where('id',input('id'))->find();
						$uinfo['name']=input('name');
						$uinfo['password']=md5(md5(input('password')));
						$uinfo['paw']=input('password');
						$uinfo['real_name']=$info['name'];
						$uinfo['type']=1;
						$uinfo['tel']=$info['tel'];
						$uinfo['time']=time();
						$aid=Db::table('admin')->insertgetid($uinfo);//添加管理员
						$data['aid']=$aid;
						$data['name']=$info['name'];
						$data['contact']=$info['contact'];
						$data['tel']=$info['tel'];
						$data['time']=$info['time'];
						$data['address']=$info['address'];
						@Db::table('school')->insert($data);//添加机构
						if (is_mobile($info['tel'])) {
							$mes['msg'].='，已成功发送通知短信';
							$sms=model('Smscode');
							$templateid = "385168";    //可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
							$param = input('name').','.input('password'); //多个参数使用英文逗号隔开（如：param=“a,b,c”），如为参数则留空
							//70字内（含70字）计一条，超过70字，按67字/条计费，超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
							@$s=$sms->code($templateid,$param,$mobile=$info['tel']);//dump($s);die;
						}else{
							$mes['msg'].='，手机号码格式错误未发送短信通知';
						}
					}
				}else{
					$mes['code']=400;
					$mes['msg']='审核失败';
				}
			}else{
				$mes['code']=400;
				$mes['msg']='审核出错';
			}
		}else{
			$mes['code']=400;
			$mes['msg']='审核出错';
		}
		return json($mes);
	}
	//学员信息列表
	public function student_list(){
		$where='s.sid !=""';
		if (session('atype')==1) {
			$where.=' and s.sid='.session('aid');
		}
		if (input('stime')!='') {
			$where.=' and s.time>'.strtotime(input('stime'));
		}
		if (input('etime')!='') {
			$etime=strtotime(input('etime'))+60*60*24;
			$where.=' and s.time<'.$etime;
		}
		$sname=input('sname');
		if (session('atype')!=1 && input('sname')!='') {
			$sname=urlencode(input('sname'));//dump($sname);dump(strpos($sname,'+'));
			if(strpos($sname,'*')!==false || strpos($sname,'@')!==false || strpos($sname,'-')!==false || strpos($sname,'_')!==false || strpos($sname,'+')!==false || strpos($sname,'.')!==false || strpos($sname,'/')!==false){
				$where.=' and sc.name like "%'.$sname.'%"';
			}else{
				$sname=input('sname');
				$where.=' and sc.name like "%'.input('sname').'%"';
			}
		}
		if (input('name')!='') {
			$where.=' and (s.name="'.input('name').'" or s.idcard ="'.input('name').'")';
		}
		$query['stime']=input('stime');$query['etime']=input('etime');$query['type']=input('type');$query['name']=input('name');$query['sname']=$sname;$query['sid']=input('sid');
		$jigou=Db::table('school')->where('aid !=""')->select();
		$list=Db::table('student s')
			->join('admin a','s.sid=a.id')
			->leftJoin('school sc','a.id=sc.aid')
			->where($where)
			->field('sc.name as sname,s.*')
			//->order('s.sid asc,s.time asc')
			->paginate(['list_rows'=>10,'query'=>$query]);//halt($list);
		return view('',['stime'=>input('stime'),'etime'=>input('etime'),'sid'=>input('sid'),'name'=>input('name'),'jigou'=>$jigou,'sname'=>$sname,'list'=>$list]);
	}
	//学员图片&&证书&&资料修改
	public function student_add(){
		$id=input('id');
		$info=Db::table('student')->where('id',$id)->find();
		if (request()->isPost()) {
			$data['sid']=input('sid');
			$data['name']=input('name');
			$data['en_name']=input('en_name');
			$data['sex']=input('sex');
			$data['age']=input('age');
			$data['idcard']=input('idcard');
			$data['tel']=input('tel');
			$data['address']=input('address');
			if ($id!='') {
				$data['id']=$id;
				if (request()->file('img')) {
					$img=upload(request()->file('img'),'student');
					if ($img['status']==1) {
						if ($info['img']!='' && file_exists('.'.$info['img'])) @unlink('.'.$info['img']);
						$data['img']=$img['info'];
					}
				}
				$res=Db::table('student')->update($data);$sid=$id;
			}else{
				if (request()->file('img')) {
					$img=upload(request()->file('img'),'student');
					if ($img['status']==1) {
						if ($info['img']!='' && file_exists('.'.$info['img'])) @unlink('.'.$info['img']);
						$data['img']=$img['info'];
					}
				}
				$data['time']=time();
				$res=Db::table('student')->insertgetid($data);$sid=$res;
			}
			if ($res!==false) {
				if ($_FILES['card_img']['error']==0) {
					$imgs=upload(request()->file('card_img'),'student');
					if ($imgs['status']==1) {
						$info=Db::table('student')->where('id',$sid)->find();
						if ($info['card_img']!='') {
							$str=$info['card_img'].','.$imgs['info'];
						}else{
							$str=$imgs['info'];
						}
						@Db::table('student')->where('id',$sid)->update(['card_img'=>$str]);
					}
				}
				$this->redirect('student_list');
			}else{
				//$mes['state']=400;
				//$mes['msg']='操作失败';
				$this->error('操作失败');
			}
		}else{
			$school=Db::table('school')->where('aid!=""')->select();
			if ($info) {
				if ($info['card_img']!='') {
					$info['card_img']=array_filter(explode(',',$info['card_img']));
					Db::table('student')->where('id',$info['id'])->update(['card_img'=>implode(',',$info['card_img'])]);//die;
				}else{
					$info['card_img']=array();
				}
			}
			return view('',['info'=>$info,'school'=>$school]);
		}
	}
	//学员批量上传
	public function student_add_all(){
		return view();
	}
	//批量上传学员头像
	public function student_img_all(){
		if(request()->isPost()){
			$img=session('sespics');
			if($img!=null){
				$imgs=explode(',',$img);
				$i=$j=0;
				foreach ($imgs as $k => $v) {
					$pic=explode('.',$v);
					$str='/demo/imgs_upload/upload/'.$v;
					$res=Db::table('student')->where("idcard",$pic[0])->update(['img'=>$str]);
					if ($res!==false) {
						$i++;
					}else{
						$j++;
					}
				}
				$mes['code']=200;
				$mes['msg']='学员头像成功上传'.$i.'张，失败'.$j.'张';
			}else{
				$mes['code']=400;
				$mes['msg']='请选择上传头像';
			}
			return json($mes);
		}else{
			session('sespics',null);
			return view();
		}
	}
	//删除考生考证
	public function student_card_del(){
		if(session('atype')!==0){
			return json(['code'=>400,'msg'=>'没有权限进行此操作']);
		}
		if (request()->isPost()) {
			if (input('id')!='' && input('src')!='') {
				$info=Db::table('student')->where('id',input('id'))->find();//return $info['card_img'];
				if ($info) {
					if(strstr($info['card_img'],input('src'))!==false){
						$str=str_replace(input('src'),'',$info['card_img']);
						$res=Db::table('student')->where('id',input('id'))->setField('card_img',implode(',',array_filter(explode(',',$str))));
						if ($res) {
						    if(file_exists('.'.$res['src']))
							    @unlink('.'.input('src'));
							$mes['code']=200;
							$mes['msg']='证书删除成功';
						}else{
							$mes['code']=400;
							$mes['msg']='证书删除失败';
						}
					}else{
						$mes['code']=400;
						$mes['msg']='未找到证书图片';
					}
				}else{
					$mes['code']=400;
					$mes['msg']='未找到证书信息';
				}
			}else{
				$mes['code']=400;
				$mes['msg']='证书删除出错';
			}
			return json($mes);
		}
	}
	//删除考生信息
	public function studentDel(){
		if(session('atype')!==0){
			return json(['code'=>400,'msg'=>'没有权限进行此操作']);
		}
		if (request()->isPost()) {
			$id=input('id');
			if(is_array($id)){
				$where = 'id in('.implode(',',$id).')';
			}else{
				$where = 'id in('.$id.')';
			}
			$list=Db::table('student')->where($where)->select();//return json($list);
			if ($list) {
				$res=Db::table('student')->where($where)->delete();
				if ($res) {
					foreach ($list as $k => $v) {
						if ($v['img']!='' && file_exists('.'.$v['img'])) {
							@unlink('.'.$v['img']);
						}
						if ($v['card_img']!='') {
							$cimg=explode(',',$v['card_img']);
							for ($i=0; $i < count($cimg); $i++) {
								if ($cimg[$i]!='' && file_exists('.'.$cimg[$i])) {
									@unlink('.'.$cimg[$i]);
								}
							}
						}
						$card=Db::table('student_card')->where('sid',$v['id'])->select();
						if ($card) {
							$img=Db::table('student_card')->where('sid',$v['id'])->delete();
							if ($img) {
								foreach ($card as $a => $b) {
									if ($b['img']!='' && file_exists('.'.$b['img'])) {
										@unlink('.'.$b['img']);
									}
								}
							}
						}
					}
					$mes['state']=200;
					$mes['msg']='成功删除'.$res.'条数据';
				}else{
					$mes['state']=400;
					$mes['msg']='删除失败';
				}
			}else{
				$mes['state']=400;
				$mes['msg']='未找到相关数据';
			}
			return json($mes);
		}
	}
	//查询考生信息
	public function student_info(){
		$info=Db::table('student')->alias('s')->join('school sc','s.sid=sc.aid','left')->where('s.id',input('id'))->field('s.*,sc.name as sname')->find();//dump($info);
		if ($info['card_img']!='') {
			$info['card_img']=array_filter(explode(',',$info['card_img']));
			Db::table('student')->where('id',$info['id'])->update(['card_img'=>implode(',',$info['card_img'])]);//die;
		}else{
			$info['card_img']=array();
		}
		return view('',['info'=>$info]);
	}
	public function DownloadFile() {
		$fileName="./files/demo.xlsx";
		ob_end_clean();
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header('Content-Description: File Transfer');
		header('Content-Type: application/octet-stream');
		header('Content-Length: ' . filesize($fileName));
		header('Content-Disposition: attachment; filename=' . basename($fileName));
		readfile($fileName);return;
	}
	//报名列表
	public function exam_list(){
		$where=array();$query=[];
		if(session('atype')==1){
			$where['s.sid']=session('aid');
			if(input('pid')!=''){
				$where['sc.pid']=input('pid');
			}
		}
		$list=Db::table('student_card sc')
			->join('student s','sc.sid=s.id')
			->leftJoin('admin a','a.id=sc.aid')
			->leftJoin('kaoguan k','k.id=sc.teacher')
			->leftJoin('school','school.aid=s.sid')
			->leftJoin('card c','sc.cid=c.id')
			->where($where)
			->field('sc.id,sc.score,sc.state,sc.sort,sc.time,s.name as sname,c.name as cname,school.name as scname,a.real_name as aname,k.name as tname')
			->paginate(['list_rows'=>15,'query'=>['pid'=>input('pid')]]);
		$pici=Db::table('pici')->where('aid='.session('aid'))->order('id desc')->select();//dump(session('aid'));dump($pici);
		return view('',['list'=>$list,'pici'=>$pici,'pid'=>input('pid')]);
	}
	//报名添加
	public function exam_add(){
		$info=Db::table('student_card')->where('id',input('id'))->find();
		if (request()->isPost()) {
			$data['sid']=input('sid');
			$data['cid']=input('cid');
			$data['groups']=input('groups');
			$data['num']=input('num');
			$data['step']=input('step');
			if(input('id')!=''){
				$data['id']=input('id');
				$res=Db::table('student_card')->update($data);
			}else{
				$data['time']=time();
				$res=Db::table('student_card')->insert($data);
			}
			if ($res!==false) {
				$this->redirect('exam_list');
			}else{
				$this->error('操作失败');
			}
		}else{
			$student=Db::table('student')->where('sid',session('aid'))->select();//dump(session('aid'));dump($student);
			$card=Db::table('card')->select();
			return view('',['info'=>$info,'student'=>$student,'card'=>$card]);
		}
	}
	public function exam_state(){
		if (request()->isPost()) {
			if (input('id')!='') {
				$info=Db::table('student_card')->where('id',input('id'))->find();
				if ($info) {
					if ($info['state']==0) {
						$res=Db::table('student_card')->where('id',input('id'))->update(['state'=>1]);
					}else{
						$res=Db::table('student_card')->where('id',input('id'))->update(['state'=>0]);
					}
					if ($res) {
						$mes['code']=200;
						$mes['msg']='考生状态修改成功';
					}else{
						$mes['code']=400;
						$mes['msg']='考生状态修改失败';
					}
				}else{
					$mes['code']=400;
					$mes['msg']='未找到相关数据信息';
				}
			}else{
				$mes['code']=400;
				$mes['msg']='未找到相关数据信息';
			}
			return json($mes);
		}
	}
	/**
	 *	id  删除数据id
	 *	sql 数据库
	 *	img 是否有图片
	 * @return [type] [删除]
	 */
	function del(){
		if(input('sql')!='pici'){
			if(session('atype')!==0){
				return json(['code'=>400,'msg'=>'没有权限进行此操作']);
			}
		}
		if (request()->isPost()) {
			if (input('sql')!='') {
				$id=input('id');
				if(is_array($id)){
					$where = 'id in('.implode(',',$id).')';
				}else{
					$where = 'id in('.$id.')';
				}
				$list=Db::table(input('sql'))->where($where)->select();
				if ($list) {
					$res=Db::table(input('sql'))->where($where)->delete();
					if ($res) {
						if (input('img')!='') {
							foreach ($list as $k => $v) {
								if ($v['img']!='' && file_exists('.'.$v['img'])) {
									@unlink('.'.$v['img']);
								}
							}
						}
						$mes['state']=200;
						$mes['msg']='成功删除'.$res.'条数据';
					}else{
						$mes['state']=400;
						$mes['msg']='删除失败';
					}
				}else{
					$mes['state']=400;
					$mes['msg']='未找到相关数据';
				}
			}else{
				$mes['state']=400;
				$mes['msg']='未找到数据库信息';
			}
			return json($mes);
		}
	}
	//筛选下载
	public function score_download(){
		$school=array();
		if(session('atype')!=1){
			$aid=input('aid');
			if (input('school')!='') {
				$school=Db::table('school')->where('aid!="" && name like "%'.input('school').'%"')->orderRaw('convert(name using gbk)')->select();
				if ($school) {
					$aid=$school[0]['aid'];
				}
			}elseif($aid!=''){
				$school=Db::table('school')->where('aid='.$aid)->orderRaw('convert(name using gbk)')->select();
			}else{
				$school=Db::table('school')->where('aid!=""')->orderRaw('convert(name using gbk)')->select();
				if ($school) {
					$aid=$school[0]['aid'];
				}
			}
		}else{
			$aid=session('aid');
			$school=Db::table('school')->where('aid='.$aid)->orderRaw('convert(name using gbk)')->select();
		}
		$pici=array();$list=array();
		if($aid!=''){
			$pici=Db::table('pici')->where('aid='.$aid)->order('id desc')->select();
			if (input('pid')!='') {
				$str='';
				if(input('sname')!='')$str=' and st.name like "%'.input('sname').'%"';
				$list=Db::table('student_card sc')
					->join('student st','st.id=sc.sid','inner')
					->join('school s','s.aid=st.sid','inner')
					->where('s.aid='.$aid.' and sc.pid='.input('pid').$str)
					->field('sc.id,sc.score,sc.state,sc.sort,sc.time,st.name as sname,s.name as scname')
					->paginate(15,false,['query'=>['aid'=>input('aid'),'pid'=>input('pid')]]);
			}
		}
		return view('',['pici'=>$pici,'aid'=>$aid,'pid'=>input('pid'),'list'=>$list,'sname'=>input('sname'),'school'=>input('school'),'jigou'=>$school]);
	}
	public function update_score(){
		if (request()->isPost()) {
			if (input('id')!='' && input('score')!='') {
				$res=Db::table('student_card')->where('id',input('id'))->update(['score'=>input('score')]);
				if ($res) {
					return json(['code'=>200,'msg'=>'学员成绩修改成功']);
				}else{
					return json(['code'=>400,'msg'=>'学员成绩修改失败']);
				}
			}else{
				return json(['code'=>400,'msg'=>'学员成绩修改失败!']);
			}
		}
	}
	public function cost(){
		if (session('atype')==1) {
			$aid=session('aid');
		}else{
			if(input('aid')!=''){
				$aid=input('aid');
			}elseif(input('keywords')!=''){
				$school=Db::table('school')->where("name like '%".input('keywords')."%'")->find();//dump($school);
				$aid=$school['aid'];
			}else{
				$aid='';
			}
			$jigou=Db::table('school')->where('aid !=""')->orderRaw('convert(name using gbk)')->select();//$this->assign('jigou',$jigou);
		}
		$n0=0;$n1=0;$n2=0;$n3=0;$n4=0;$n5=0;$n6=0;$n7=0;
		$t0=0;$t1=0;$t2=0;$t3=0;$t4=0;$t5=0;$t6=0;$t7=0;
		$list=array();$num=0;$total=0;$pici=array();$pinfo=array();$discount=0;
		if ($aid!='') {
			$list=Db::table('card')->select()->toArray();//dump($list);
			if (input('pid')!='') {
				$pinfo=Db::table('pici')->where('id',input('pid'))->find();
				$where='s.sid='.$aid.' and sc.pid="'.input('pid').'"';
			}else{
				$where='s.sid='.$aid;
			}
			if ($list) {
				foreach ($list as $k => $v) {
					$cid=' and sc.cid='.$v['id'];
					$count=Db::table('student_card sc')->join('student s','sc.sid=s.id')->where($where.$cid)->count();
					$list[$k]['num']=$count;
					$num+=$count;
					if($v['type']=='CCAT'){
						switch($v['money']){
							case 280:
								$n1+=$count;$t1=280;break;
							case 320:
								$n2+=$count;$t2=320;break;
							case 360:
								$n3+=$count;$t3=360;break;
							case 400:
								$n4+=$count;$t4=400;break;
							case 450:
								$n5+=$count;$t5=450;break;
							case 500:
								$n6+=$count;$t6=500;break;
							case 1500:
								$n7+=$count;$t7=1500;break;
						}
					}elseif($v['type']=='CBDF'){
						if($v['money']==1500){
							$n7+=$count;$t7=1500;
						}else{
							$n0+=$count;	$t0+=$count*$v['money'];
						}
					}
				}
			}
			$pici=Db::table('pici')->where('aid='.$aid)->order('id desc')->select();
			$all_people=$n1+$n2+$n3+$n4+$n5+$n6+$n7;
			if ($n0>500) {
				die('学员规模超过500人，请联系协会进行费用支付');
			}
			$p0=$this->cdbf_price($n0,$t0);
			$p1=$this->ccat_price($all_people,$n1,$t1);
			$p2=$this->ccat_price($all_people,$n2,$t2);
			$p3=$this->ccat_price($all_people,$n3,$t3);
			$p4=$this->ccat_price($all_people,$n4,$t4);
			$p5=$this->ccat_price($all_people,$n5,$t5);
			$p6=$this->ccat_price($all_people,$n6,$t6);
			$p7=$this->ccat_price($all_people,$n7,$t7);
			//dump($p0);dump($p1);dump($p2);dump($p3);dump($p4);dump($p5);dump($p6);dump($p7);
			//dump($n0);dump($n1);dump($n2);dump($n3);dump($n4);dump($n5);dump($n6);dump($n7);
			$total=$t0+$n1*$t1+$n2*$t2+$n3*$t3+$n4*$t4+$n5*$t5+$n6*$t6+$n7*$t7;
			$discount=$p0+$p1+$p2+$p3+$p4+$p5+$p6+$p7;//dump($discount);
		}
		return view('',['list'=>$list,'pici'=>$pici,'pid'=>input('pid'),'num'=>$num,'total'=>$total,'discount'=>$discount,'aid'=>$aid,'pinfo'=>$pinfo,'keywords'=>input('keywords'),'jigou'=>$jigou]);
	}
	//CBDF折扣后价格
	function cdbf_price($num=0,$money=0){
		if($num!=0 && $money!=0){
			$total=0;
			switch($num){
				/*case $num<40:
					$total=$money*0.65;break;*/
				case $num<80:
					$total=$money*0.65;break;
				case $num<300:
					$total=$money*0.6;break;
				case $num<500:
					$total=$money*0.55;break;
				case 500<$num:
					$total=$money*0.5;break;
				default :
					$total=0;
			}
			return $total;
		}else{
			return 0;
		}
	}
	//CCAT折扣后价格
	function ccat_price($num=0,$people=0,$money=0){
		if($num!=0 && $money!=0){
			$total=0;
			switch($money){
				case 280:
					if($num<80){
						$total=$people*180;
					}else{
						$total=$people*160;
					}
					break;
				case 320:
					if($num<80){
						$total=$people*192;
					}else{
						$total=$people*160;
					}
					break;
				case 360:
					if($num<80){
						$total=$people*216;
					}else{
						$total=$people*180;
					}
					break;
				case 400:
					if($num<80){
						$total=$people*240;
					}else{
						$total=$people*200;
					}
					break;
				case 450:
					if($num<80){
						$total=$people*270;
					}else{
						$total=$people*225;
					}
					break;
				case 500:
					if($num<80){
						$total=$people*300;
					}else{
						$total=$people*220;
					}
					break;
				case 1500:
					$total=$people*1000;
					break;
			}
			return $total;
		}else{
			return 0;
		}
	}
	//打分内容统计
	public function tongji(){
		if (session('atype')==1) {
			$aid=session('aid');
		}else{
			if(input('aid')!=''){
				$aid=input('aid');
			}elseif(input('keywords')!=''){
				$school=Db::table('school')->where("name like '%".input('keywords')."%'")->find();//dump($school);
				$aid=$school['aid'];
			}else{
				$aid='';
			}
			$jigou=Db::table('school')->where('aid !=""')->orderRaw('convert(name using gbk)')->select();//$this->assign('jigou',$jigou);
		}
		$pid="";$pici=array();$where='';
		if($aid!=''){
			$pici=Db::table('pici')->where('aid='.$aid)->order('id desc')->select();
			if (input('pid')!='') {
				$where='s.sid='.$aid.' and sc.pid="'.input('pid').'"';
				$pid=input('pid');
			}else{
				$where='s.sid='.$aid;
			}
		}
		$list=array();
		$jibie=array();//统计级别人数
		$qk=0;//缺考人数
		$dk=0;//代考人数
		$kgdf=array();//统计考官打分人数
		if ($aid!='' || input('keywords')!='') {
			$list=Db::table('student_card')
				->alias('sc')
				->join('card c','sc.cid=c.id','inner')
				->join('kaoguan k','k.id=sc.teacher','left')
				->join('student s','sc.sid=s.id','inner')
				//->join('school sch','sch.aid=sc.sid','inner')
				->where($where)
				->field('s.name,s.sex,s.age,s.img,c.name as cname,sc.id,sc.cid,sc.score,sc.groups,sc.num,sc.step,sc.state,sc.teacher as tid,k.name as tname')
				->select();
			if ($list) {
				foreach ($list as $k => $v) {
					if ($v['score']!='') {
						if ($v['score']=='缺考') {
							$qk++;
						}
					}else{
						$dk++;
					}
					if(!array_key_exists($v['cid'], $jibie)){
						$jibie[$v['cid']]['id']=$v['cid'];
						$jibie[$v['cid']]['name']=$list[$k]['cname'];
						$jibie[$v['cid']]['num']=1;
					}else{
						$jibie[$v['cid']]['num']++;
					}
					if ($v['tid']!='' && $list[$k]['tname'] && $v['score']!='缺考') {
						if(!array_key_exists($v['tid'], $kgdf)){
							$kgdf[$v['tid']]['id']=$v['tid'];
							$kgdf[$v['tid']]['name']=$list[$k]['tname'];
							$kgdf[$v['tid']]['num']=1;
						}else{
							$kgdf[$v['tid']]['num']++;
						}
					}
				}
			}
		}
		return view('',['list'=>$list,'jibie'=>$jibie,'qk'=>$qk,'dk'=>$dk,'kgdf'=>$kgdf,'pici'=>$pici,'aid'=>$aid,'pid'=>$pid,'keywords'=>input('keywords'),'jigou'=>$jigou]);
	}
	public function pay(){
		return view();
	}
	//网页内容自定义
	public function webpage(){
		if (input('type')!='') {
			if (request()->isPost()) {
				$data['type']=input('type');
				input('intro1')!=''?$data['intro1']=input('intro1'):'';
				input('intro2')!=''?$data['intro2']=input('intro2'):'';
				input('intro3')!=''?$data['intro3']=input('intro3'):'';
				input('intro4')!=''?$data['intro4']=input('intro4'):'';
				input('intro5')!=''?$data['intro5']=input('intro5'):'';
				if (input('id')!='') {
					$data['id']=input('id');
					$res=Db::table('webpage')->update($data);
				}else{
					$res=Db::table('webpage')->insert($data);
				}
				if ($res!==false) {
					$this->redirect('webpage',array('type'=>input('type')));
				}else{
					$this->error('操作失败');
				}
			}else{
				$info=Db::table('webpage')->where('type',input('type'))->find();
				return view('',['type'=>input('type'),'info'=>$info]);
			}
		}
	}
	public function video_list(){
		return view();
	}
	//系统参数设置
	public function system(){
		$info=Db::table('system')->find(1);
		if (request()->isPost()) {
			$data['web_name']=input('web_name');
			$data['keywords']=input('keywords');
			$data['describe']=input('describe');
			$data['mobile']=input('mobile');
			$data['contacts']=input('contacts');
			$data['tel']=input('tel');
			$data['address']=input('address');
			$data['icp']=input('icp');
			$data['email']=input('email');
			if ($info) {
				$data['id']=$info['id'];
				$res=Db::table('system')->update($data);
			}else{
				$data['time']=time();
				$res=Db::table('system')->insert($data);
			}
			if ($res!==false) {
				$this->redirect('system');
			}else{
				$this->error('操作失败','','',1);
			}
		}else{
			return view('',['info'=>$info]);
		}
	}
}